package com.stylefeng.guns.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.constant.dictmap.DotDict;
import com.stylefeng.guns.common.constant.dictmap.PushDict;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.DeptMapper;
import com.stylefeng.guns.common.persistence.dao.DotMapper;
import com.stylefeng.guns.common.persistence.dao.UserMapper;
import com.stylefeng.guns.common.persistence.model.Dept;
import com.stylefeng.guns.common.persistence.model.Dot;
import com.stylefeng.guns.common.persistence.model.User;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.page.PageInfoBT;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.warpper.PushWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.common.persistence.model.Push;
import com.stylefeng.guns.modular.system.service.IPushService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自动通知控制器
 *
 * @author fengshuonan
 * @Date 2018-01-16 20:53:08
 */
@Controller
@RequestMapping("/push")
public class PushController extends BaseController {

    private String PREFIX = "/system/push/";

    @Autowired
    private IPushService pushService;

    @Autowired
    DotMapper dotMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    DeptMapper deptMapper;

    /**
     * 跳转到自动通知首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "push.html";
    }

    /**
     * 跳转到添加自动通知
     */
    @RequestMapping("/push_add")
    public String pushAdd(Model model) {
        List <Dot> dots=pushService.getUndistriDots(ShiroKit.getUser().getDeptId());
        Dept dept=deptMapper.selectById(ShiroKit.getUser().getDeptId());
        if(dept==null){
            model.addAttribute("sms_num",0);
            model.addAttribute("call_num",0);
        }else{
            model.addAttribute("sms_num",dept.getMessage_num()==null?"0":dept.getMessage_num());
            model.addAttribute("call_num",dept.getCall_num()==null?"0":dept.getCall_num());
        }
        model.addAttribute("dots",dots);

        return PREFIX + "push_add.html";
    }


    /**
     * 跳转到添加自动通知
     */
    @RequestMapping("/pushDetil")
    public String pushDetil(Model model) {
        Page<Map<String, Object>> page=new Page(0,1000);
        List<Map<String, Object>> dots=dotMapper.selectDots(page,null,"",ShiroKit.getUser().getDeptId());
        model.addAttribute("dots",dots);

        User user=userMapper.selectById(ShiroKit.getUser().getId());

        EntityWrapper<Push> wrapper = new EntityWrapper<Push>();
        wrapper.addFilter("deptid = {0}", user.getDeptid());
        Dept dept=deptMapper.selectById(ShiroKit.getUser().getDeptId());
        if(dept==null){
            model.addAttribute("sms_num",0);
            model.addAttribute("call_num",0);
        }else{
            model.addAttribute("sms_num",dept.getMessage_num()==null?"0":dept.getMessage_num());
            model.addAttribute("call_num",dept.getCall_num()==null?"0":dept.getCall_num());
        }

        List<Push> pushes= pushService.selectList(wrapper);
        if(pushes.size()>0){
            Push push = pushService.selectById(pushes.get(0).getId());
            model.addAttribute("item",push);
            LogObjectHolder.me().set(push);
            return PREFIX + "push_edit.html";
        }

        return PREFIX + "push_add.html";
    }

    /**
     * 跳转到修改自动通知
     */
    @RequestMapping("/push_update/{pushId}")
    public String pushUpdate(@PathVariable Integer pushId, Model model) {
        Push push = pushService.selectById(pushId);
        model.addAttribute("item",push);
        Page<Map<String, Object>> page=new Page(0,1000);
        List<Map<String, Object>> dots=dotMapper.selectDots(page,null,"",ShiroKit.getUser().getDeptId());
        model.addAttribute("dots",dots);
        Dept dept=deptMapper.selectById(ShiroKit.getUser().getDeptId());
        if(dept==null){
            model.addAttribute("sms_num",0);
            model.addAttribute("call_num",0);
        }else{
            model.addAttribute("sms_num",dept.getMessage_num()==null?"0":dept.getMessage_num());
            model.addAttribute("call_num",dept.getCall_num()==null?"0":dept.getCall_num());
        }
        LogObjectHolder.me().set(push);
        return PREFIX + "push_edit.html";
    }

    /**
     * 获取自动通知列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,Integer limit, Integer offset) {

        Page<Map<String, Object>> page=new Page(offset,limit);
        PageInfoBT pageInfoBT=new PageInfoBT(page);
        Map m=new HashMap();
        m.put("deptid",ShiroKit.getUser().getDeptId());
        m.put("condition",condition);
        List<Map<String, Object>> push = pushService.list(page,m);
        pageInfoBT.setRows(push);
        pageInfoBT.setTotal(page.getTotal());
        return pageInfoBT;
    }

    /**
     * 新增自动通知
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "新增通知", key = "dotName", dict = PushDict.class)
    @ResponseBody
    public Object add(Push push) {

        EntityWrapper<Push> wrapper = new EntityWrapper<Push>();
        wrapper.addFilter("deptid = {0}", ShiroKit.getUser().getDeptId());
        List<Push> pushes= pushService.selectList(wrapper);
        if (pushes.size()>0) {
            throw new GunsException(BizExceptionEnum.PUSH_EXISTED);
        }
        push.setDeptid(ShiroKit.getUser().getDeptId());
        pushService.insert(push);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除自动通知
     */
    @RequestMapping(value = "/delete")
    @BussinessLog(value = "删除通知", key = "dotName", dict = PushDict.class)
    @ResponseBody
    public Object delete(@RequestParam Integer pushId) {
        pushService.deleteById(pushId);
        return SUCCESS_TIP;
    }

    /**
     * 修改自动通知
     */
    @RequestMapping(value = "/update")
    @BussinessLog(value = "修改通知", key = "dotName", dict = PushDict.class)
    @ResponseBody
    public Object update(Push push) {
        pushService.updateById(push);
        return super.SUCCESS_TIP;
    }

    /**
     * 自动通知详情
     */
    @RequestMapping(value = "/detail/{pushId}")
    @ResponseBody
    public Object detail(@PathVariable("pushId") Integer pushId) {
        return pushService.selectById(pushId);
    }
}
